﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ToolPositioningInternal.CApTPIDataGenerator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ToolPositioningInternal;

internal class CApTPIDataGenerator
{
  public Action<ITxObject> GenerationStarted;
  public Action<ITxObject> GenerationFinished;
  public Action<ITxObject> GeneratingDataExampleCallBack;
  private CApTPIObjectManipulator _objManipulator = new CApTPIObjectManipulator();
  private string _outputDirectory;
  private ImageFormat _imageFormat = ImageFormat.Bmp;

  public CApTPIDataGenerator(string outputDirectory)
  {
    this.OutputDirectory = outputDirectory;
    this.LastSavedImagesPath = new List<string>();
  }

  public ITxLocatableObject Object
  {
    get => this._objManipulator.Object;
    set => this._objManipulator.Object = value;
  }

  public string OutputDirectory
  {
    get => this._outputDirectory;
    set
    {
      if (string.IsNullOrEmpty(value) || !(value != this._outputDirectory))
        return;
      this._outputDirectory = "";
      if (!Directory.Exists(value))
        Directory.CreateDirectory(value);
      this._outputDirectory = value;
    }
  }

  public ImageFormat ImageFormat => this._imageFormat;

  public List<string> LastSavedImagesPath { get; set; }

  public bool IsGenerationInProgress
  {
    get => this._objManipulator.IsManipulationInProgress;
    set => this._objManipulator.IsManipulationInProgress = value;
  }

  public TxVector ObjectApproachDirection => this._objManipulator.ObjectLargestSideOrthogonalVector;

  protected static string CreateUniqueFilename(string filename)
  {
    DateTime now = DateTime.Now;
    string format = "yyyyMMdd_hhmmss_fff";
    filename = $"{filename}__{now.ToString(format)}";
    return filename;
  }

  public async Task GenerateAsync(int imagesCount)
  {
    Action<ITxObject> generationStarted = this.GenerationStarted;
    if (generationStarted != null)
      generationStarted((ITxObject) this.Object);
    await Task.Run((Action) (() =>
    {
      try
      {
        this._objManipulator.Rotate(imagesCount, this.GeneratingDataExampleCallBack);
      }
      catch
      {
      }
      Action<ITxObject> generationFinished = this.GenerationFinished;
      if (generationFinished == null)
        return;
      generationFinished((ITxObject) this.Object);
    }));
  }

  public void Generate(int imagesCount)
  {
    try
    {
      Action<ITxObject> generationStarted = this.GenerationStarted;
      if (generationStarted != null)
        generationStarted((ITxObject) this.Object);
      this._objManipulator.Rotate(imagesCount, this.GeneratingDataExampleCallBack);
    }
    catch
    {
    }
    Action<ITxObject> generationFinished = this.GenerationFinished;
    if (generationFinished == null)
      return;
    generationFinished((ITxObject) this.Object);
  }

  public void SaveImage(Image bitmap, string filename, string subfolder)
  {
    string str = Path.Combine(this._outputDirectory, subfolder);
    if (!Directory.Exists(str))
      Directory.CreateDirectory(str);
    string filename1 = $"{Path.Combine(str, CApTPIDataGenerator.CreateUniqueFilename(filename))}.{this.ImageFormat.ToString().ToLower()}";
    try
    {
      bitmap.Save(filename1, this.ImageFormat);
    }
    catch
    {
    }
    this.LastSavedImagesPath.Add(filename1);
  }
}
